package test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import junit.framework.TestCase;
import atg.nucleus.Nucleus;
import atg.nucleus.NucleusTestUtils;

import component.ComponentToBeTested;

/**
 * A simple JUnit test case using ATG Dust tools testing a custom component.
 * 
 * @author: jarryzhou
 * @version: 1.0, Oct 24, 2016
 */
public class FirstTestCase extends TestCase {

    /**
     * A test method to create properties file for component, add the properties
     * file into ATG initial path and start Nucleus. Finally it checks the field
     * value set by the properties file in the component we created.
     * 
     * @throws Exception
     */
    public void testComponent() throws Exception {
        // Read or create the basic configuration directory.
        final File configpath = NucleusTestUtils.getConfigpath(this.getClass(), "config");
        // Create a properties file, this file is used to initialize the
        // Component in which we set a value in the component field.
        final Properties props = new Properties();
        props.put("value", "This is a test!");
        // Create properties file in the configuration directory,
        // NucleusTestUtils will write the class & properties into the
        // properties file.
        final File propFile = NucleusTestUtils.createProperties("test/ComponentToBeTested", configpath,
                "component.ComponentToBeTested", props);
        propFile.deleteOnExit();
        // Set the ATG initial component list.
        final List<String> initial = new ArrayList<String>();
        initial.add("/test/ComponentToBeTested");
        NucleusTestUtils.createInitial(configpath, initial);
        // Start Nucleus with the basic configuration directory.
        final Nucleus nucleus = NucleusTestUtils.startNucleus(configpath);

        // Test whether Nucleus & the component starts correctly or not.
        ComponentToBeTested testComponent = null;
        try {
            testComponent = (ComponentToBeTested) nucleus.resolveName("/test/ComponentToBeTested");
            assertNotNull("Could not resolve test componet", testComponent);
            assertNotNull("Read component value, it is " + testComponent.getValue(), testComponent.getValue());
        } finally {
            nucleus.stopService();
            assertNotNull(testComponent);
            testComponent = null;
        }

    }

}